package com.lanou3g.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou3g.bean.Compony;
import com.lanou3g.mapper.ComponyMapper;
import com.lanou3g.service.ComponyService;
import com.lanou3g.util.Result;

@Service
public class ComponyServiceImpl implements ComponyService {

	@Autowired
	private ComponyMapper componyMapper;
	
	@Override
	public Result getComponies(Integer page, Integer limit) {
		List<Compony> componies = componyMapper.getComponies();
		PageHelper.startPage(page, limit);
		PageInfo<Compony> pageInfo = new PageInfo<>(componies);
		Result result = new Result(0,"获取公司列表信息成功");
		result.setCount(pageInfo.getTotal());
		result.setData(pageInfo.getList());
		
		return result;
	}

	@Override
	public Result getComponyByComponyName(String componyName) {
		Compony compony = componyMapper.getComponyByComponyName(componyName);
		if(compony == null) {
			//如果没有找到compony,说明数据库没这个公司
			return new Result(0,"没有找到名字是"+componyName+"的公司");
		}else {
			return new Result(1,"这个公司已经存在!");
		}
	}

	@Override
	public Result insCompony(Compony compony) {
		int rows = componyMapper.insCompony(compony);
		if(rows != 0) {
			return new Result(0,"添加成功!");
		}else {
			return new Result(1,"添加失败!");
		}
	}

	@Override
	public Compony findComponyById(Integer id) {
		return componyMapper.findComponyById(id);
	}

	@Override
	public Result updateCompony(Compony compony) {
		int rows = componyMapper.updateCompony(compony);
		if(rows == 0) {
			//如果更新成功的条数为0,即更新失败
			return new Result(1,"更新失败!");
		}else {
			return new Result(0,"更新成功!");
		}
	}

	@Override
	public Result deleteComponyById(Integer id) {
		int rows = componyMapper.deleteComponyById(id);
		if(rows == 0) {
			//说明没删除掉任何数据
			return new Result(1,"删除失败!");
		}else {
			return new Result(0,"删除成功!");
		}
	}

	@Override
	public List<Compony> getComponies() {
		return componyMapper.getComponies();
	}

}
